#include "sensor2worker.h"
#include "qthread.h"
#include <QDateTime>

Sensor2Worker::Sensor2Worker(ushort di, ushort d0, QObject *parent)
    : SensorWorker(di, d0, parent) {}

void Sensor2Worker::stop()
{
    m_running = false;
}

void Sensor2Worker::process()
{
    clearInputEdge();

    while (m_running) {
        ushort level = 0;
        if (checkInputLevel(level) && level < 1) {
            handleFallingEdge();
        }
        QThread::msleep(1);
    }
    emit finished();
}

void Sensor2Worker::handleFallingEdge()
{
    emit log("[" + QDateTime::currentDateTime().toString() +
             "] [DI]" + QString::number(m_di) + " : Falling edge trigger");

    // 输出持续信号
    setOutputBit(0);
    emit log("[" + QDateTime::currentDateTime().toString() +
             "] [DO]" + QString::number(m_do) + " : Continuous output");
}
